package com.xy.tianyu.module.novel.fragment

import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.novel.adapter.CompetitiveManMutilpleAdapter
import com.xy.tianyu.module.novel.bean.CompetitiveManMultipleItem
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendBean
import com.xy.tianyu.module.novel.bean.CompetitiveRecommendMultipleItem
import com.xy.tianyu.network.Api
import com.xy.tianyu.network.ApiParams
import kotlinx.android.synthetic.main.fragment_competitive_man.*
import rxhttp.RxHttp
import rxhttp.toClass

/**
 * @time 2020/11/3 14:55
 * @class 描述:
 */
class CompetitiveManFragment : LazyLoadBaseFragment() {
    private lateinit var manAdapter:CompetitiveManMutilpleAdapter
    private var competitiveManList = mutableListOf<CompetitiveManMultipleItem>()
    override fun setContentView(): Int {
        return R.layout.fragment_competitive_man
    }

    override fun lazyLoad() {
        rxLifeScope.launch({
            val competitiveRecommendBean = getBookCompetitiveRecommend()
            val dataList = competitiveRecommendBean.data
            dataList!!.removeAt(7)
            if (!dataList.isNullOrEmpty()){
                for ((index,data) in dataList.withIndex()){
                    val multipleItem = when (index){
                        0 ->  {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_ONE,
                                data!!
                            )
                        }
                        1 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_TWO,
                                data!!
                            )
                        }
                        2 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_THREE,
                                data!!
                            )
                        }
                        3 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_FOUR,
                                data!!
                            )
                        }
                        4 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_FIVE,
                                data!!
                            )
                        }
                        5 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_SIX,
                                data!!
                            )
                        }
                        6 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_SEVEN,
                                data!!
                            )
                        }
                        7 -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_EIGHT,
                                data!!
                            )
                        }
                        else -> {
                            CompetitiveManMultipleItem(CompetitiveManMultipleItem.COMPETITIVE_MAN_NINE,
                                data!!
                            )
                        }
                    }
                    competitiveManList.add(multipleItem)

                    ry_man.layoutManager = LinearLayoutManager(activity)
                    manAdapter = CompetitiveManMutilpleAdapter(competitiveManList)
                    ry_man.adapter = manAdapter
                }
            }
        },{},{},{})
    }

    override fun stopLoad() {

    }

    private suspend fun getBookCompetitiveRecommend(): CompetitiveRecommendBean {
        return RxHttp.get(Api.AI_KAN_SHU_FREE_NOVEL_BASE_URL+ Api.AI_KAN_SHU_FREE_NOVEL_COMPETITIVE_URL)
            .addAllHeader(ApiParams.getNovelHeaders())
            .add("type",1)
            .toClass<CompetitiveRecommendBean>()
            .await()
    }
}